package cloud.maque.admin.controller;

import cloud.maque.admin.annotations.TenantId;
import cloud.maque.admin.annotations.TenantQuery;
import cloud.maque.admin.domain.entity.MqMenu;
import cloud.maque.admin.domain.request.MenuRoleRequest;
import cloud.maque.admin.domain.vo.MenuDetailVo;
import cloud.maque.admin.domain.vo.MenuVo;
import cloud.maque.admin.service.MqMenuService;
import cloud.maque.admin.service.MqRolePrivilegeService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cloud.maque.framework.common.response.ResponseBodyVo;
import cloud.maque.framework.security.ssoclient.utils.SsoUserInfoUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单接口
 *
 * @author zhuyf
 * @date 2023/3/17 20:57
 */
@RestController
@RequestMapping(value = "/menu")
@Api(tags = "运维-菜单接口")
public class MenuController {


    @Autowired
    MqMenuService menuService;

    @Autowired
    SsoUserInfoUtils infoUtils;

    @Autowired
    MqRolePrivilegeService rolePrivilegeService;


    /**
     * 获取用户的菜单
     *
     * @return
     */
    @GetMapping("/nav")
    @ApiOperation(value = "获取用户的菜单")
    public ResponseBodyVo<List<MenuVo>> NavMenu() {
        String userId = infoUtils.getUserId();
        return ResponseBodyVo.Success(menuService.getUserMenu(userId));
    }

    @PostMapping("/list")
    @ApiOperation(value = "菜单查询")
    @TenantQuery
    public ResponseBodyVo<PageInfo<MqMenu>> page(@RequestBody @ApiParam("查询内容") PaginateDataResquest<MqMenu> pageReq) {
        return ResponseBodyVo.Success(menuService.page(pageReq));
    }

    @PostMapping("/tree/{root}")
    @ApiOperation(value = "菜单树")
    @TenantQuery
    public ResponseBodyVo<List<MenuVo>> MenuTree(@RequestBody @ApiParam("查询内容") PaginateDataResquest<MqMenu> pageReq, @PathVariable("root") String root) {
        return ResponseBodyVo.Success(menuService.MenuTree(pageReq, root));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "菜单详情")
    @TenantQuery
    public ResponseBodyVo<MenuDetailVo> Detail(@RequestParam("id") String menuId, @TenantId String tenantId) {
        return ResponseBodyVo.Success(menuService.getDetail(menuId, tenantId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增或修改")
    public ResponseBodyVo<MenuDetailVo> save(@RequestBody @ApiParam("内容") MenuDetailVo mqMenu) {
        return ResponseBodyVo.Success(menuService.saveMenu(mqMenu));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "菜单删除")
    public ResponseBodyVo<Boolean> Delete(@PathVariable("id") String menuId) {
        return ResponseBodyVo.Success(menuService.delete(menuId));
    }


    @PostMapping("/addRoles")
    @ApiOperation(value = "给菜单添加角色")
    public ResponseBodyVo addRoles(@RequestBody MenuRoleRequest menuRoleRequest) {
        rolePrivilegeService.addRoleForMenu(menuRoleRequest);
        return ResponseBodyVo.Success();
    }


}
